package com.yjhb.android.sbz.activity;

import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.View;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.RefreshRecordAdapter;
import com.yjhb.android.sbz.entity.FlushRecord;
import com.yjhb.android.sbz.mvp.refreshrecord.RefreshRecordPresenter;
import com.yjhb.android.sbz.mvp.refreshrecord.RefreshRecordView;
import com.yjhb.android.sbz.tool.LoadingDialogTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.EmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/7 14:39
 */
public class RefreshRecordActivity extends BaseActivity<RefreshRecordPresenter> implements OnRefreshLoadmoreListener, RefreshRecordView {
    private final String TAG = "RefreshRecordActivity";

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.layout_empty)
    View layout_empty;
    @BindView(R.id.recyclerView)
    EmptyRecyclerView recyclerView;

    RefreshRecordAdapter refreshRecordAdapter;

    private final int size = 25;
    private int current = 1;
    List<FlushRecord> records = new ArrayList<>();

    @Override
    protected RefreshRecordPresenter initPresenter() {
        return new RefreshRecordPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_refresh_record;
    }

    @Override
    protected void initUI() {
        recyclerView.setEmptyView(layout_empty);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(refreshRecordAdapter = new RefreshRecordAdapter(this));
        refreshRecordAdapter.setData(records);
        loadingDialog = LoadingDialogTool.createDialog(this);
    }

    @Override
    protected void initData() {
        loadingDialog.show();
        presenter.getFlushHistory(size, current);
    }

    @Override
    protected void regEvent() {
        smartRefreshLayout.setOnRefreshLoadmoreListener(this);
    }

    @OnClick(R.id.back)
    public void back() {
        finish();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        presenter.getFlushHistory(size, ++current);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        current = 1;
        presenter.getFlushHistory(size, current);
    }

    @Override
    public void onLoad(List<FlushRecord> records) {
        if (records != null)
            this.records.addAll(records);
        refreshRecordAdapter.notifyDataSetChanged();
        smartRefreshLayout.finishLoadmore();
    }

    @Override
    public void onRefresh(List<FlushRecord> records) {
        this.records.clear();
        if (records != null)
            this.records.addAll(records);
        refreshRecordAdapter.notifyDataSetChanged();
        loadingDialog.cancel();
        smartRefreshLayout.finishRefresh();
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadmore();
        ZxToast.showBottomToastLight("似乎出了点问题~~QAQ");
    }
}
